package com.example.time;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.drawable.AnimatedImageDrawable;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.zip.DataFormatException;

public class MainActivity extends AppCompatActivity {

    private SimpleDateFormat sdf;
    private String open;
    private String close;
    private ImageView main_iv_fans;
    private AnimationDrawable background;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        open = "08:00";
        close = "23:58";

        main_iv_fans = findViewById(R.id.main_iv_fans);

        main_iv_fans.setBackgroundResource(R.drawable.fans);
        background = (AnimationDrawable) main_iv_fans.getBackground();


        fansDate();
    }

    private void fansDate() {
        Date date = new Date();

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");
        SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("HH:mm");

        try {
            Date openTime = simpleDateFormat.parse(open);
            Date closeTime = simpleDateFormat.parse(close);
            String format = simpleDateFormat1.format(date);
            Date parse = simpleDateFormat.parse(format);
            System.out.println(openTime);
            System.out.println(closeTime);
            System.out.println(format);
            System.out.println(parse);
            Log.e("当前时间", String.valueOf(date));

//            String openTimeStr = simpleDateFormat.format(openTime);
//            String closeTimestr = simpleDateFormat.format(closeTime);
//            String nowTimestr = simpleDateFormat.format(date);
//            Log.e("三个日期format","openTimeStr:"+ openTimeStr +"closeTimestr:"+closeTimestr+"nowTimestr:"+nowTimestr);

            if(date.getTime() > openTime.getTime() && date.getTime() < closeTime.getTime()){
                background.stop();
            }else{
                background.start();
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        
    }
}